//
//  UIAlertController+Extension.swift
//  mmda
//
//  Created by 李陆涛 on 2018/4/19.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit

extension UIAlertController {
    
    class func alert(title: String?, message: String?, commit:(title:String,hander:((UIAlertAction) -> Swift.Void)? )? = nil,cancel:(
        title:String,hander:((UIAlertAction) -> Swift.Void)? )? = nil) {
        
        guard let vc = UIApplication.shared.keyWindow?.rootViewController else { return }
        
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
        
        if let commit = commit {
           
            let commitAction = UIAlertAction(title: commit.title, style: UIAlertActionStyle.default, handler: commit.hander)
            
            alert.addAction(commitAction)
            alert.preferredAction = commitAction
            
        }
        
        if let cancel = cancel {
            
            let cancelAction = UIAlertAction(title: cancel.title, style: UIAlertActionStyle.cancel, handler: cancel.hander)
            
            alert.addAction(cancelAction)
            
        }
        
        alert.show(vc, sender: nil)
        
    }
    
}
